package fr.isima.gui;

import java.sql.Date;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import fr.isima.R;
import fr.isima.data.TrackProviderAdapter;
import fr.isima.model.Track;

public class TrackSaving extends Activity
{

    private static final String  TAG = "TrackEndRecord";

    private SharedPreferences    preferences;

    private TrackProviderAdapter adapter;

    private Button               buttonSave;

    private String               trackName;

    private String               trackPath;

    private Date                 date;

    private int                  minFreqRefresh;

    private int                  minDistRefresh;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.track_saving);

        preferences = getSharedPreferences(TrackPreferences.USER_PREFERENCE,
                Activity.MODE_PRIVATE);
        trackName = preferences.getString(TrackPreferences.PREF_TRACK_NAME, "");
        trackPath = preferences.getString(TrackPreferences.PREF_TRACK_PATH, "");
        int indexFreqRefresh = preferences.getInt(
                TrackPreferences.PREF_MIN_FREQ_REFRESH, 0);
        int indexDistRefresh = preferences.getInt(
                TrackPreferences.PREF_MIN_DIST_REFRESH, 0);
        date = new Date(preferences
                .getLong(TrackPreferences.PREF_TRACK_DATE, 0));
        adapter = new TrackProviderAdapter(getContentResolver());

        // Get the option values from the arrays.
        Resources r = getResources();
        int[] freqValues = r.getIntArray(R.array.min_freq_values);
        int[] distValues = r.getIntArray(R.array.min_dist_values);

        // Convert the values to ints.
        minFreqRefresh = freqValues[indexFreqRefresh];
        minDistRefresh = distValues[indexDistRefresh];

        buttonSave = (Button) findViewById(R.id.buttonSave);

        buttonSave.setOnClickListener(new OnClickListener()
        {

            public void onClick(View v)
            {
                Intent main = new Intent(v.getContext(), IsiHiking.class);
                saveTrack();
                resetPreferences();
                startActivity(main);
            }
            
        });
    }

    private void resetPreferences()
    {
        Editor editor = preferences.edit();
        editor.putString(TrackPreferences.PREF_TRACK_NAME, TrackPreferences.DEFAULT_TRACK_NAME);
        editor.putInt(TrackPreferences.PREF_MIN_FREQ_REFRESH, TrackPreferences.DEFAULT_FREQ_REFRESH);
        editor.putInt(TrackPreferences.PREF_MIN_DIST_REFRESH, TrackPreferences.DEFAULT_DIST_REFRESH);
        editor.commit();
    }
    
    private void saveTrack()
    {
        Track track = new Track(trackName, trackPath, date, minFreqRefresh,
                minDistRefresh);
        adapter.insertTrack(track);
        Log.d(TAG, "Track " + track + " inserted");

    }

}
